package com.andyzhou.astatine;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.huaban.analysis.jieba.JiebaSegmenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Translate {
    @RequiresApi(api = Build.VERSION_CODES.O)
    public String translate(String toTranslate) {
        JiebaSegmenter segmenter = new JiebaSegmenter();
        System.out.println(segmenter.sentenceProcess(toTranslate));
        final String[] reservedKeywords = {
                "{", // 如果要翻译的句子中包含特殊格式，则把特殊格式转义

                "了别", "了当", "了得", "了断",
                "了无", "了愿", "了债", "了办",
                "了事", "了然", "了却", "了结",
                "了解", "了如", "了不得", "了不起",
                "说了算", "铁了心", "铁了事", "开了窍",
                "没了", "乱了套", "横了心", "不了",
                "不甚了然", "知了", "完了", "算了",
                "罢了", "肿么了", "过奖了", "好极了",
                "大不了", "免不了",

                "地面", "的地", "有地", "地球",
                "地空", "地名", "地貌", "地区",
                "地理", "地块", "地形", "地势",
                "地位", "地图", "地铁", "地狱",
                "地盘", "地皮", "地坪", "地上",
                "地点", "地带", "地方", "地缝",
                "地瓜", "地火", "地精", "地库",
                "地雷", "地址", "地震", "地质",
                "地下", "地产", "地层", "地板",
                "地表", "地暖", "土地", "天地",
                "田地", "境地", "圈地", "外地",
                "洼地", "入地", "异地", "盆地",
                "平地", "湿地", "圣地", "实地",
                "腹地", "各地", "工地", "高地",
                "耕地", "该地", "海地", "基地",
                "绝地", "接地", "空地", "落地",
                "领地", "绿地", "陆地", "林地",
                "之地", "阵地", "占地", "质地",
                "驻地", "场地", "产地", "此地",
                "草地", "本地", "白地", "内地",
                "拿地", "农地", "墓地", "某地",

                "得体", "得意", "得到", "得分",
                "得过", "得很", "得奖", "得空",
                "得了", "得证", "得知", "得罪",
                "得主", "得出", "取得", "赢得",
                "使得", "锻得", "懂得", "获得",
                "觉得", "解得", "记得", "懒得",
                "值得", "显得", "测得", "变得",
                "不得", "彼得", "难得", "没得",
                "免得",

                "可的", "打的", "的卢",

                // Comments from https://bilibili.com/
                "考古", "梦开始的地方", "泪目", "十个泪目九个笑，还有一个在狂笑",
                "十个泪目一个笑，还有九个在狂笑", "公屏",
                "核善", "核平", "核蔼", "以核为贵",
                "蘑菇蛋", "蘑菇弹", "以核服人", "以德服人",
                "以理服人",
                "有生之年", "爷青回", "爷青结", "失踪人口回归",
                "up主", "UP主", "Up主", "零零后",
                "00后", "鸽视频", "阿婆主",
                "富贵色", "会员色",
                "字母君", "字幕菌", "野生字幕菌",
                "哈哈哈哈", "红红火火恍恍惚惚",

                "啊我死了", "awsl", "AWSL", "Awsl",
                "阿伟死了", "阿伟瘦了", "啊我是驴",

                "武汉加油", "有内味了", "双厨狂喜", "爷青回",
                "禁止套娃",

                "xswl", "XSWL", "Xswl", "hhh",
                "lol", "LOL", "Lol", "rofl",
                "Rofl", "ROFL", "Hahaha", "hahaha",
                "HaHaHa", "HAHAHA",

                "哔哩哔哩", "bilibili", "Bilibili", "BiliBili",
                "BILIBILI",

                // ...（和谐）
                "发生肾么事了", "发生甚么事了",
                "把颈椎练坏了",
                "年轻人不讲武德", "不讲武德", "有备而来",
                "来骗", "来偷袭", "耗子尾汁",
                "闪电五连鞭",
                "金坷垃", "奥利给",

                "亿遍", "亿点点",
                "橘里橘气", "紫气东来", "磕到了",
        };
        HashMap<String, String[]> translationMap = new HashMap<String, String[]>();
        translationMap.put("什么", new String[]{"啥", "啥子", "甚么", "肾么"});
        translationMap.put("怎么", new String[]{"咋"});
        translationMap.put("炒饭", new String[]{"抄饭", "吵饭"});
        translationMap.put("充气", new String[]{"冲气"});
        translationMap.put("零售", new String[]{"另售"});
        translationMap.put("装潢", new String[]{"装璜", "装黄"});
        translationMap.put("盒饭", new String[]{"合饭"});
        translationMap.put("菠萝", new String[]{"波罗"});
        translationMap.put("鸡蛋", new String[]{"鸡旦"});
        translationMap.put("停车", new String[]{"仃车"});
        translationMap.put("零", new String[]{"〇"});
        translationMap.put("秘籍", new String[]{"秘笈"});
        translationMap.put("快餐", new String[]{"快歺"});
        toTranslate = toTranslate.trim();
        if (toTranslate.equals("")) {
            return "输入内容不能为空！";
        }

        int strLen = toTranslate.length();
        boolean onlyQuestionMark = Pattern.matches("^([？?])$", toTranslate);
        if (onlyQuestionMark) {
            return "您只输入了一个问号！";
        }
        if (toTranslate.endsWith("？")) {
            toTranslate = toTranslate.substring(0, strLen - "？".length());
        } else if (toTranslate.endsWith("?")) {
            toTranslate = toTranslate.substring(0, strLen - 1);
        }
        String result;
        String pattern = "(.*?是)((啥|什么)(玩意|东西|意思)?(儿|呢|呀|啊)?|谁)$";
        if (Pattern.matches(pattern, toTranslate)) {
            Pattern compiled = Pattern.compile(pattern);
            Matcher m = compiled.matcher(toTranslate);
            if (m.group(2).equals("谁")) {
                result = toTranslate;
            } else {
                if (m.group(4).equals("意思")) {
                    result = m.group(1) + "什么";
                } else {
                    result = m.group(1) + "什么意思";
                }
            }
            return result;
        }
        result = toTranslate;
        for (int i = 0; i < reservedKeywords.length; i++) {
            result = result.replace(reservedKeywords[i], "{k@#" + i + "}");
        }
        List<String> words;
        words = segmenter.sentenceProcess(toTranslate);
        for (Map.Entry<String, String[]> entry: translationMap.entrySet()) {
            for (String informal : entry.getValue()) {
                for (int wordIndex = 0; wordIndex < words.size(); wordIndex++) {
                    if (words.get(wordIndex).equals(informal)) {
                        words.set(wordIndex, entry.getKey());
                    }
                }
            }
        }
        result = String.join("", words);
        for (int i = 0; i < reservedKeywords.length; i++) {
            result = result.replace("{k@#" + i + "}", reservedKeywords[i]);
        }
        return result;
    }
}
